// pages/review/index.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        categoryLsit: [],
        total: 0,
        balance: 0,
        remarks: 0,
        userName: '', // 修改人
        updateTime: '', // 修改时间
        isEdit: false,
        nowTime: '',
        admin: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getResultData();
        wx.hideShareMenu();
        this.setData({
          admin: wx.getStorageSync('admin')
        })
        let date = new Date();

        // 获取年份
        let year = date.getFullYear();

        // 获取月份，注意月份是从0开始的，所以需要加1
        let month = date.getMonth() + 1;

        // 获取日期
        let day = date.getDate();

        // 获取小时
        let hours = date.getHours();

        // 获取分钟
        let minutes = date.getMinutes();

        // 获取秒数
        let seconds = date.getSeconds();

        // 格式化月份、日期、分钟和秒数，如果小于10则在前面补0
        month = month < 10 ? '0' + month : month;
        day = day < 10 ? '0' + day : day;
        hours = hours < 10 ? '0' + hours : hours;
        minutes = minutes < 10 ? '0' + minutes : minutes;
        seconds = seconds < 10 ? '0' + seconds : seconds;

        // 组合成需要的格式
        const formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
        this.setData({ nowTime: formattedDate, balance: options.balance });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    getResultData() {
        wx.showLoading();
        wx.request({
            url: `${app.globalData.baseUrl}/api/classify/selProofreading`,
            method: 'GET',
            header: {
                'token': wx.getStorageSync('token'),
                "Content-Type": "application/json"
            },
            success: (res) => {
                if (res.data.code == 0) {
                    const data = res.data.data;
                    console.log(data.categoryLsit)
                    this.setData({
                        categoryLsit: data.categoryLsit,
                        total: data.total,
                        remarks: data.remarks,
                        userName: data.userName || data.createBy, // 修改人
                        updateTime: data.updateTime || data.createTime, // 修改时间
                    });
                    wx.hideLoading();
                } else {
                    wx.showToast({
                        title: '系统错误',
                    });
                    wx.hideLoading();
                }
            },
            fail: () => {
                wx.hideLoading();
                wx.showToast({
                    title: '系统错误',
                });
            }
        })
    },

    onEdit() {
        if (!this.data.isEdit) this.setData({ isEdit: true });
        else {
            this.commit();
        }
    },

    onCancel() {
        this.setData({ isEdit: false });
    },

    onAdd() {
        const arr = this.data.categoryLsit;
        arr.push({ name: '', money: '',  storage: ''});
        this.setData({ categoryLsit: arr });
    },

    del(e) {
        wx.showModal({
          title: '警告',
          content: '该操作会删除此项，您确定要删除吗？',
          complete: (res) => {
            if (res.cancel) {
                
            }
        
            if (res.confirm) {
                const arr = this.data.categoryLsit;
                arr.splice(e.currentTarget.dataset.index, 1);
                this.setData({ categoryLsit: arr });
            }
          }
        })
    },

    nameChange(e) {
        const nameIndex = e.currentTarget.dataset.index;
        const arr = this.data.categoryLsit;
        console.log(arr)
        arr.forEach((item, index) => {
            if (index == nameIndex) {
                item.name = e.detail.value;
            }
        });
        this.setData({ categoryLsit: arr });
    },

    moneyChange(e) {
        const nameIndex = e.currentTarget.dataset.index;
        const arr = this.data.categoryLsit;
        let total = 0;
        arr.forEach((item, index) => {
            if (index == nameIndex) {
                item.money = e.detail.value;
            }
            if (item.money && !isNaN(item.money)) total += parseFloat(item.money);

        });
        this.setData({ categoryLsit: arr, total: total.toFixed(2) });
    },

    remarksChange(e) {
        this.setData({ remarks: e.detail.value });
    },

    commit() {
        const that = this;
        wx.showModal({
            content: '是否确认修改',
            cancelText: '关闭',
            success: function (sm) {
                if (sm.confirm) {
                    wx.request({
                        url: `${app.globalData.baseUrl}/api/classify/updateProofreading`,
                        method: "POST",
                        data: {
                            categoryLsit: that.data.categoryLsit,
                            total: that.data.total,
                            balance: that.data.balance,
                            remarks: that.data.remarks,
                            id: 1
                        },
                        header: {
                            "Content-Type": "application/json",
                            'token': wx.getStorageSync('token'),
                        },
                        success: function (res) {
                            if (res.data.code == 0) {
                                that.setData({ isEdit: false });
                                that.getResultData();
                            } else {
                                wx.showToast({
                                    title: res.data.msg || '系统异常',
                                })
                            }
                        },
                        fail() {
                            wx.showToast({
                                title: '出错啦！',
                                icon: 'error',
                                duration: 2000
                            })
                        }
                    })
                } else {
                    console.log('用户点击取消');
                }
            }
        });
    },

})